# -*- coding: utf-8 -*-
import logging

from pylons import request, response, session, tmpl_context as c, url, app_globals as g
from pylons.controllers.util import abort, redirect
from repoze.what.predicates import is_user, has_permission
from pylons.decorators.cache import beaker_cache
from craneserver.lib.base import BaseController, render
from craneserver.lib.helpers import flash
import simplejson

log = logging.getLogger(__name__)

class SessionController(BaseController):

    def login(self):
        #render login page or redirect to login page which could be served by nginx or apache
        identity = request.environ.get('repoze.who.identity')
        if identity:
            redirect(url(controller='app',action='index'))
        else:
            return render('login.mako')

    def verifyLogin(self):
        identity = request.environ.get('repoze.who.identity')
        return simplejson.dumps({'login_succeeded': identity is not None})

    def verifyLogout(self):
        redirect(url(controller='session',action='login'))
